package top.mschen.blog.controller.test;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.*;

@RestController
public class DemoFeign {


    @RequestMapping("hi")
    public String sayHiFromClientOne(@RequestParam(value = "name") String name){
      return "hi"+name;
    }


    public static void main(String[] args) {
        /*1、准备数据**/
        SkuVo sku1 = new SkuVo(1L,"p1",100L);
        SkuVo sku2 = new SkuVo(2L,"p2",101L);
        SkuVo sku3 = new SkuVo(3L,"p3",102L);
        SkuVo sku4 = new SkuVo(3L,"p4",103L);
        SkuVo sku5 = new SkuVo(2L,"p5",100L);
        SkuVo sku6 = new SkuVo(5L,"p6",100L);

        List<SkuVo> skuVoList = Arrays.asList(new SkuVo [] {sku1,sku2,sku3,sku4,sku5,sku6});

        /*2、分组算法**/
        Map<Long, List<SkuVo>> skuIdMap = new HashMap<>();
        for (SkuVo skuVo : skuVoList) {
            List<SkuVo> tempList = skuIdMap.get(skuVo.getSkuId());
            /*如果取不到数据,那么直接new一个空的ArrayList**/
            if (tempList == null) {
                tempList = new ArrayList<>();
                tempList.add(skuVo);
                skuIdMap.put(skuVo.getSkuId(), tempList);
            }
            else {
                /*某个sku之前已经存放过了,则直接追加数据到原来的List里**/
                tempList.add(skuVo);
            }
        }

        /*3、遍历map,验证结果**/
        for(Long skuId : skuIdMap.keySet()){
            System.out.println(skuIdMap.get(skuId));
        }
    }

    private static class SkuVo {

        private Long skuId;
        private Long skuId1;
        private String skuId2;

        public SkuVo(Long skuId, String skuId2, Long skuId1) {
            this.skuId = skuId;

            this.skuId2 = skuId2;
            this.skuId1 = skuId1;
        }

        public Long getSkuId() {
            return skuId;
        }

        public void setSkuId(Long skuId) {
            this.skuId = skuId;
        }

        public Long getSkuId1() {
            return skuId1;
        }

        public void setSkuId1(Long skuId1) {
            this.skuId1 = skuId1;
        }

        public String getSkuId2() {
            return skuId2;
        }

        public void setSkuId2(String skuId2) {
            this.skuId2 = skuId2;
        }

        @Override
        public String toString() {
            return "SkuVo{" +
                    "skuId=" + skuId +
                    ", skuId1=" + skuId1 +
                    ", skuId2='" + skuId2 + '\'' +
                    '}';
        }
    }


    @RequestMapping("test")
    public String test(@RequestBody String id,@RequestBody List<String> List){
        return ""+id+ List.get(0);
    }

    @RequestMapping("test1")
    public String test1(HttpServletRequest request,String a,int b){

        String bodyString = getBodyString(request);
        return a+b;
    }

    @RequestMapping("test2")
    public String test2(@RequestParam("list") List<String> list){
        System.out.println();
        return JSONObject.toJSONString(list);
    }


    /**
     * 获取请求Body
     *
     * @param request
     * @return
     */
    public static String getBodyString(ServletRequest request) {
        StringBuilder sb = new StringBuilder();
        InputStream inputStream = null;
        BufferedReader reader = null;
        try {
            inputStream = request.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

//    @Autowired
//    private SgArticleService SgArticleServiceImpl;
//
//    @GetMapping("testCache")
//    @ResponseBody
//    public List<SgArticle> testCache(){
//        return  SgArticleServiceImpl.selectSgArticeList();
//    }
}
